package com.yf1706.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yf1706.bean.CheckDetail;
import com.yf1706.dao.jpa.ICheckDetailsJDAO;
import com.yf1706.dao.jpa.IEmpJDAO;
import com.yf1706.dao.mybatis.IEmpMDAO;

@Service
public class inexcelService {
	public static List<CheckDetail> lits;
	@Autowired 
	private ICheckDetailsJDAO cdJDao;
	@Autowired
	private IEmpJDAO eJdao;
	@Autowired
	private IEmpMDAO eMdao;
	//数据导入操作，定义导入的版本型号 
		 public static boolean isExcel2003(String filePath) {
				return filePath.matches("^.+\\.(?i)(xls)$");
			}

			public static boolean isExcel2007(String filePath) {
				return filePath.matches("^.+\\.(?i)(xlsx)$");
			}
	//将导入的数据转换为集合存储起来
			public List<CheckDetail> importData(File file) {
				Workbook wb = null;
				inexcelService rs=new inexcelService();
				List<CheckDetail> list=new ArrayList<>();
				try {
					if (inexcelService.isExcel2007(file.getPath())) {
						wb = new XSSFWorkbook(new FileInputStream(file));
					} else {
						wb = new HSSFWorkbook(new FileInputStream(file));
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
				Sheet sheet = wb.getSheetAt(0);// 获取第一张表
				for (int i = 1; i < sheet.getLastRowNum()+1; i++) {
					Row row = sheet.getRow(i);// 获取索引为i的行，以1开始
					int detailsId=(int)row.getCell(0).getNumericCellValue();
					int empId=(int)row.getCell(1).getNumericCellValue();
					Date cardTime1=(Date) row.getCell(2).getDateCellValue();
					long cardTime =cardTime1.getTime();
					System.out.println(cardTime);
					String reason=row.getCell(3).getStringCellValue();
					String whetherValid=row.getCell(4).getStringCellValue();			
					CheckDetail r1=new CheckDetail();
					r1.setDetailsId(detailsId);
					r1.setEmp(eMdao.queryBasicById(empId));
					r1.setCardTime(new Timestamp(cardTime));
					r1.setReason(reason);
					r1.setWhetherValid(whetherValid);					
					list.add(r1);
				}
				try {
					wb.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				lits=list;
				return list;
			}
			
			/*public String improts(int eid) {
				CheckDetail e=cdJDao.findById(eid).get();
				if(lits.size()!=0&&lits!=null) {;
					for(CheckDetail r1:lits) {
						cdJDao.save(r1);
					}
					lits.clear();
					return "ok";
				}else {
					lits.clear();
					return "no";
				}
			}*/
			

}
